package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlMxetjf2jlbBsDetail;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlMxetjf2jlbBsDetailVo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlMxetjf2jlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlMxetjf2jlbBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlMxetjf2jlbBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlMxetjf2jlbBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlMxetjf2jlbBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlMxetjf2jlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料马歇尔试验检测记录(体积法)2Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlMxetjf2jlbBsInfoServiceImpl implements IReportLqhhlMxetjf2jlbBsInfoService {

    private final ReportLqhhlMxetjf2jlbBsInfoMapper baseMapper;
    private final ReportLqhhlMxetjf2jlbBsDetailMapper detailMapper;

    /**
     * 查询沥青混合料马歇尔试验检测记录(体积法)2
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlMxetjf2jlbBsInfoVo queryById(Long excelId){
        ReportLqhhlMxetjf2jlbBsInfoVo reportLqhhlMxetjf2jlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLqhhlMxetjf2jlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlMxetjf2jlbBsDetail>().eq(ReportLqhhlMxetjf2jlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            reportLqhhlMxetjf2jlbBsInfoVo.setDetailList(detailList);
        }
        return reportLqhhlMxetjf2jlbBsInfoVo;
    }

    /**
     * 查询沥青混合料马歇尔试验检测记录(体积法)2列表
     */
    @Override
    public TableDataInfo<ReportLqhhlMxetjf2jlbBsInfoVo> queryPageList(ReportLqhhlMxetjf2jlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlMxetjf2jlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlMxetjf2jlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料马歇尔试验检测记录(体积法)2列表
     */
    @Override
    public List<ReportLqhhlMxetjf2jlbBsInfoVo> queryList(ReportLqhhlMxetjf2jlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlMxetjf2jlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlMxetjf2jlbBsInfo> buildQueryWrapper(ReportLqhhlMxetjf2jlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlMxetjf2jlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlMxetjf2jlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlMxetjf2jlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlMxetjf2jlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqhhlMxetjf2jlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlMxetjf2jlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlMxetjf2jlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlMxetjf2jlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlMxetjf2jlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlMxetjf2jlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlMxetjf2jlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlMxetjf2jlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqhhlMxetjf2jlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqhhlMxetjf2jlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqhhlMxetjf2jlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlMxetjf2jlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlMxetjf2jlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlMxetjf2jlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getLiqyl() != null, ReportLqhhlMxetjf2jlbBsInfo::getLiqyl, bo.getLiqyl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiswd()), ReportLqhhlMxetjf2jlbBsInfo::getJiswd, bo.getJiswd());
        lqw.eq(StringUtils.isNotBlank(bo.getHuanjwd()), ReportLqhhlMxetjf2jlbBsInfo::getHuanjwd, bo.getHuanjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportLqhhlMxetjf2jlbBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiscs()), ReportLqhhlMxetjf2jlbBsInfo::getJiscs, bo.getJiscs());
        lqw.eq(bo.getLilzdxdmd() != null, ReportLqhhlMxetjf2jlbBsInfo::getLilzdxdmd, bo.getLilzdxdmd());
        lqw.eq(bo.getKuanglhcmtjxdmd() != null, ReportLqhhlMxetjf2jlbBsInfo::getKuanglhcmtjxdmd, bo.getKuanglhcmtjxdmd());
        lqw.eq(bo.getLiqdxdmd() != null, ReportLqhhlMxetjf2jlbBsInfo::getLiqdxdmd, bo.getLiqdxdmd());
        lqw.eq(bo.getHeckldyxxdmd() != null, ReportLqhhlMxetjf2jlbBsInfo::getHeckldyxxdmd, bo.getHeckldyxxdmd());
        lqw.eq(bo.getKuanglhcbgxdmd() != null, ReportLqhhlMxetjf2jlbBsInfo::getKuanglhcbgxdmd, bo.getKuanglhcbgxdmd());
        lqw.eq(bo.getLiqjhlbl() != null, ReportLqhhlMxetjf2jlbBsInfo::getLiqjhlbl, bo.getLiqjhlbl());
        lqw.eq(bo.getYouxlqyl() != null, ReportLqhhlMxetjf2jlbBsInfo::getYouxlqyl, bo.getYouxlqyl());
        lqw.eq(bo.getJildbbmjzh() != null, ReportLqhhlMxetjf2jlbBsInfo::getJildbbmjzh, bo.getJildbbmjzh());
        lqw.eq(bo.getLiqmyxhd() != null, ReportLqhhlMxetjf2jlbBsInfo::getLiqmyxhd, bo.getLiqmyxhd());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlMxetjf2jlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlMxetjf2jlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlMxetjf2jlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlMxetjf2jlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlMxetjf2jlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlMxetjf2jlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlMxetjf2jlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlMxetjf2jlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料马歇尔试验检测记录(体积法)2
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlMxetjf2jlbBsInfoBo bo) {
        ReportLqhhlMxetjf2jlbBsInfo add = BeanUtil.toBean(bo, ReportLqhhlMxetjf2jlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLqhhlMxetjf2jlbBsDetail> details = new ArrayList<>(16);
            for (int i=1;i<=16;i++ ){
                details.add(new ReportLqhhlMxetjf2jlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料马歇尔试验检测记录(体积法)2
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqhhlMxetjf2jlbBsInfoBo bo) {
        ReportLqhhlMxetjf2jlbBsInfo update = BeanUtil.toBean(bo, ReportLqhhlMxetjf2jlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportLqhhlMxetjf2jlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlMxetjf2jlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料马歇尔试验检测记录(体积法)2
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
